<?php
/*	  
 Copyright (C) 2011  João Dias de Carvalho Neto

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once '../DBConnection.php';

class Profile
{
	private $number=0;
	private $name;	
	private $status;
	
	public function getNumber()
	{
		return $this->number;
	}
	
	public function getName()
	{
		return $this->name;
	}
	
	public function setName($value)
	{
		$this->name = $value;
	}
	
	public function isActive()
	{
		return $this->status;
	}
	
	public function setActive($value)
	{
		$this->status = $value;
	}
	
	public function persistObject()
	{
		$num = $this->getNumber();
		$nam = $this->getName();
		$act = ($this->isActive()?'active':'inactive');
		
		$db = new DBConnection();
		
		if ($this->getNumber()==0)
		{
			$sql = " REPLACE users (userLogin,userKind,userStatus) 
				     VALUES
				     ($nam,'profile',$act)
			   	   ";
		}
		else
		{
			$sql = " REPLACE users (userNumber,userLogin,userKind,userStatus) 
				     VALUES 
				     ($num,$nam,'profile',$act) ";
		}		
		$db->executeNonRead($sql);		
	}
	
	public function deleteObject()
	{
		$sql = " DELETE FROM users WHERE userNumber=".$this->getNumber();
		$db = new DBConnection();
		$db->executeNonRead($sql);		
	}

	public function loadObjectById($number)
	{
		$sql = " SELECT 
					userNumber,
					userLogin,
					userStatus
				 FROM
					users
				 WHERE
				 		userNumber=$number
				 AND	userKind='profile'
				 	
			   ";
		$db = new DBConnection();
		$ret = $db->executeScalar($sql);
		$this->number = $ret['userNumber'];
		$this->setName($ret['userLogin']);
		$this->setActive($ret['userActive']);
	}
	
}

?>